﻿using System.Collections.Generic;
using System.Linq;
using Microsoft.AspNetCore.Mvc;
using ZilLion.Service.WeChatMall.Domain.Responses;
using ZilLion.Service.WeChatMall.Domain.Services.Interface;

namespace ZilLion.Service.WeChatMall.Controllers
{
    /// <summary>
    ///     微商城店铺
    /// </summary>
    [Route("Api/WechatMall/Shop")]
   
    public class ShopController : WechatMallControllerBase
    {
        private readonly IShopInfoService _shopInfoService;

        public ShopController(IShopInfoService shopInfoService)
        {
            _shopInfoService = shopInfoService;
        }

        /// <summary>
        ///     获取商铺信息
        /// </summary>
        /// <param name="token"></param>
        /// <returns></returns>
        [Route("getAllShops")]
        [HttpGet]
        public ShopListResponse GetShops(string token = null)
        {
            var appid = GetAppId();

            var list = _shopInfoService.GetShopsByAppid(appid);

            var adcodes =
                from shop in list.ToList()
                group shop by shop.Adcode
                into adcodeGroup
                select adcodeGroup.Key;


            var result = new List<ShopGroupDto>();
            foreach (var adcode in adcodes)
                if (adcode.IsNotNullOrEmpty())
                {
                    var tmplist = list.Where(x => x.Adcode.Left(4) == adcode.Left(4));
                    var dto = new ShopGroupDto {Adcode = adcode.Left(4), Shops = tmplist.ToList()};
                    result.Add(dto);
                }
            return new ShopListResponse {ShopGroups = result};
        }


        /// <summary>
        ///     获取商铺信息
        /// </summary>
        /// <param name="shopid"></param>
        /// <param name="token"></param>
        /// <returns></returns>
        [Route("GetShop")]
        [HttpGet]
        public ShopResponse GetShop(long shopid,string token = null)
        {
            var dto = _shopInfoService.GetShopById(shopid);
            return new ShopResponse(){Shop = dto};
        }
    }
}